/**
 * Simple proxy objects interface compatible with
 * cross-domain Jsl.Ajax
 * 
 * @author  Mykhailo Stadnyk <mikhus@gmail.com>
 * @package Jsl
 * @version 1.0
 */
Jsl.Ajax.Proxy = Jsl.extend( Object, {

	targetUrl    : null,
	proxyUrl     : null,
	jsVarName    : '__jsl_var__',
	proxyVarName : '__jsl_proxy_var__',
	proxyUrlName : '__jsl_proxy_url__',
	phpCompat    : false,
	scope        : window,

	/**
	 * Class constructor
	 * 
	 * @param {Object} config
	 */
	constructor : function( config) {
		Jsl.Ajax.Proxy.$super.constructor.call( this);
		var config = config || {};

		Jsl.apply( this, config);

		if (config.proxyUrl && Jsl.Url.isValid( config.proxyUrl))
			this.proxyUrl = new Jsl.Url( config.proxyUrl)
		; else if (!Jsl.Url.isValid( config.proxyUrl))
			throw new Error( 'Jsl.Ajax.Proxy: given "proxyUrl" is invalid!')
		;

		if (config.targetUrl)
			this.setTargetUrl( config.targetUrl)
		;
	},

	/**
	 * Sets a target Url for this proxy
	 * 
	 * @param  {String} url
	 * @return {Jsl.Ajax.Proxy}
	 */
	setTargetUrl : function( url) {
		this.targetUrl = url;
		return this;
	},

	/**
	 * Returns a fully-qualified proxy URL
	 * 
	 * @return {String}
	 */
	getUrl : function() {
		var url = new Url( this.proxyUrl, this.phpCompat);
		url.query.set( this.proxyVarName, this.jsVarName);
		url.query.set( this.proxyUrlName, this.targetUrl);
		return url;
	},

	/**
	 * Returns a response text obtained from proxy
	 * 
	 * @return {String}
	 */
	getResponseText : function() {
		return this.scope[this.jsVarName] || '';
	},

	/**
	 * Returns DOM XML document or null on failure or if document is empty
	 * 
	 * @return {XMLDocument}
	 */
	getResponseXml : function() {
		var doc = null;
		try { doc = (new Jsl.Xml).parse( this.getResponseText()).getDom(); } catch (e) {}
		return doc;
	}

});
